package cc.mxtt.mvc.service.impl;

import java.util.List;

import cc.mxtt.mvc.model.Dept;
import cc.mxtt.mvc.model.TreeGridDept;
import cc.mxtt.mvc.model.User;
import cc.mxtt.mvc.service.DeptService;

public class DeptServiceImpl implements DeptService{
	
	@Override
	public Dept findById(String id) {
		return Dept.dao.findById(id);
	}
    
	@Override
	public boolean create(Dept dept) {
		return dept.save();
	}

	@Override
	public boolean update(Dept dept) {
		return dept.update();
	}

	@Override
	public boolean delete(String id) {
		return Dept.dao.deleteById(id);
	}
	
	@Override
	public boolean existsUser(String dept_id) {
		String sql = "select * from sys_user where dept_id=?";
		User user = User.dao.findFirst(sql, dept_id);
		if (user==null){
			return false;
		}
		return true;
	}
	
	@Override
	public boolean existsDept(String dept_id) {
		String sql ="select * from sys_dept where parent_id=?";
		Dept dept = Dept.dao.findFirst(sql, dept_id);
		if (dept==null){
			return false;
		}
		return true;
	}
	
	@Override
	public List<TreeGridDept> getTreeGrid(String id) {
		return Dept.dao.getTreeGridListDept(Dept.dao.getParentListDept(id));
	}

	@Override
	public boolean isLock(String id) {
		// TODO Auto-generated method stub
		System.out.println("start");
		String sql = "select * from sys_dept where id=?";
		Dept  dept = Dept.dao.findFirst(sql, id);
		int lock = dept.getInt("lock");
		System.out.println("lock:"+lock);
		
	
		return lock == 1;
	}

	@Override
	public boolean lock(String id) {
		// TODO Auto-generated method stub
		String sql = "select * from sys_dept where id=?";
		Dept  dept = Dept.dao.findFirst(sql, id);
		dept = dept.set("lock", 1);
		
		return dept.update();
	}

	@Override
	public boolean unLock(String id) {
		String sql = "select * from sys_dept where id=?";
		Dept  dept = Dept.dao.findFirst(sql, id);
		dept = dept.set("lock", 0);
		return dept.update();
	}



}
